home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Java / SlideShow / Sources (complete) / ImageButton.java < prev    next >
Encoding:
Java Source  |  2000-09-28  |  7.2 KB  |  280 lines  |  [TEXT/CWIE]

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.util.Hashtable;
  4.  
  5. /**
  6.  * An abstract class to implement the core features of a component with
  7.  * button like behavior, and the capability to display several images
  8.  * representing different states of the button.
  9.  */
  10. public abstract class ImageButton extends Component
  11. {
  12.     //Declare data members
  13.     //Insert "ImageButton data members"
  14.     protected Hashtable imageHash;
  15.     protected Image image;
  16.     protected String imageName;
  17.     protected boolean isMouseDown = false;
  18.     protected boolean isMouseInside = false;
  19.     protected String actionCommand;
  20.     protected ActionListener actionListener = null;
  21.     
  22.     /**
  23.      * Constructs a default instance of this class.
  24.      */
  25.     public ImageButton()
  26.     {
  27.         //REGISTER_LISTENERS
  28.         //Insert "ImageButton register listeners"
  29.         Mouse aMouse = new Mouse();
  30.         this.addMouseListener(aMouse);
  31.         
  32.         //Initialize state information
  33.         //Insert "ImageButton init state"
  34.         imageHash = new Hashtable();
  35.         actionCommand = "ImageButton Action";
  36.     }
  37.  
  38.     /**
  39.      * Gets called when the mouse button is released on this button.
  40.      * @param isMouseInside, if true, the mouse is located inside the button area,
  41.      * if false the mouse is outside the button area.
  42.      */
  43.     protected void handleMouseRelease(boolean isMouseInside)
  44.     {
  45.         //Handle firing an ActionEvent to our listeners if the mouse was released
  46.         //inside the button.
  47.         //Insert "ImageButton handleMouseReleased"
  48.         if (isMouseInside)
  49.             fireActionEvent();
  50.     }
  51.  
  52.     /**
  53.      * Gets called when the mouse crosses into or out of the button area.
  54.      * @param isMouseInside, is true if the mouse is in the button area,
  55.      * false if it is outside.
  56.      * @param isMouseDown, is true if the mouse button is pressed, false if it is not.
  57.      */
  58.     protected abstract void handleRollover(boolean isMouseInside, boolean isMouseDown);
  59.     
  60.     /**
  61.      * Gets called when the mouse button is pressed on this button.
  62.      */
  63.     protected abstract void handleMousePressed();
  64.  
  65.  
  66.     /**
  67.      * Adds an image to the button.
  68.      * @param imagePath, the location of the image resource to use.
  69.      * This path is relative to the location of this class file.
  70.      * @param imageName, the name used to identify the image for later
  71.      * use in this button.
  72.      * @see #removeImage
  73.      */
  74.     public void addImage(String imagePath, String imageName)
  75.     {
  76.         //Handle storing the information in our internal data structure.
  77.         //Insert "ImageButton addImage"
  78.         if (imageName != null && !imageName.equals(""))
  79.         {
  80.             Image newImage = Misc.loadImage(imagePath, this, true);
  81.             if (newImage != null)
  82.             {
  83.                 imageHash.put(imageName, newImage);
  84.             }
  85.         }
  86.     }
  87.     
  88.     /**
  89.      * Removes an image from the button
  90.      * @param imageName, the identifying name of the image to remove.
  91.      * @see #addImage
  92.      */
  93.     public void removeImage(String imageName)
  94.     {
  95.         //Handle removing the image from our internal data structure.
  96.         //Insert "ImageButton removeImage"
  97.         if (imageName != null && !imageName.equals(""))
  98.         {
  99.             imageHash.remove(imageName);
  100.         }
  101.     }
  102.     
  103.     /**
  104.      * Sets the image for the button to use as its current image.
  105.      * @param imageName, the identifying name of the image to use.
  106.      */
  107.     public void setImage(String imageName)
  108.     {
  109.         //Handle locating the image in our internal data structure,
  110.         //setting it as the current image, and repainting the button.
  111.         //Insert "ImageButton setImage"
  112.         if (imageName != null && !imageName.equals(""))
  113.         {
  114.             Image temp = (Image)imageHash.get(imageName);
  115.             if (temp != null)
  116.             {
  117.                 image = temp;
  118.                 this.imageName = imageName;
  119.                 repaint();
  120.             }
  121.         }
  122.     }
  123.     
  124.     /**
  125.      * Gets the name of the image currently in use.
  126.      * @return The identifying name of the image being used.
  127.      */
  128.     public String getImage()
  129.     {
  130.         //Return the current image name.
  131.         //Insert "ImageButton getImage"
  132.         return imageName;
  133.     }
  134.     
  135.     /**
  136.      * Gets the actual Image Object which is currently being used.
  137.      * @return The java.awt.Image currently in use.
  138.      */
  139.     public Image getImageObject()
  140.     {
  141.         //Return the current image object.
  142.         //Insert "ImageButton getImageObject"
  143.         return image;
  144.     }
  145.  
  146.     
  147.     //Routines for handling ActionListener management.
  148.     //Insert "ImageButton Action Management"
  149.     /**
  150.      * Sets the command name of the action event fired by this button.
  151.      * @param command The name of the action event command fired by this button
  152.      */
  153.     public void setActionCommand(String command)
  154.     {
  155.         actionCommand = command;
  156.     }
  157.  
  158.     /**
  159.      * Returns the command name of the action event fired by this button.
  160.      * @return the action command name
  161.      */
  162.     public String getActionCommand()
  163.     {
  164.         return actionCommand;
  165.     }
  166.  
  167.     /**
  168.      * Adds the specified action listener to receive action events
  169.      * from this button.
  170.      * @param l the action listener
  171.      */
  172.     public void addActionListener(ActionListener l)
  173.     {
  174.         actionListener = AWTEventMulticaster.add(actionListener, l);
  175.     }
  176.  
  177.     /**
  178.      * Removes the specified action listener so it no longer receives
  179.      * action events from this button.
  180.      * @param l the action listener
  181.      */
  182.     public void removeActionListener(ActionListener l)
  183.     {
  184.         actionListener = AWTEventMulticaster.remove(actionListener, l);
  185.     }
  186.     
  187.     /**
  188.      * Fire an action event to the listeners.
  189.      */
  190.     protected void fireActionEvent()
  191.     {
  192.         if (actionListener != null)
  193.             actionListener.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, actionCommand));
  194.     }
  195.  
  196.     /** 
  197.      * Returns the preferred size of this component.
  198.      * @see #getMinimumSize
  199.      * @see LayoutManager
  200.      */
  201.     public Dimension getPreferredSize()
  202.     {
  203.         //If the current image is not null, then return the size of the image.
  204.         //If it is null, defer to the super class.
  205.         //Insert "ImageButton getPreferredSize"
  206.         if (image != null)
  207.             return new Dimension(image.getWidth(this), image.getHeight(this));
  208.         
  209.         return super.getPreferredSize();
  210.     }
  211.  
  212.     /** 
  213.      * Paints the component.  This method is called when the contents
  214.      * of the component should be painted in response to the component
  215.      * first being shown or damage needing repair.  The clip rectangle
  216.      * in the Graphics parameter will be set to the area which needs
  217.      * to be painted.
  218.      * @param g the specified Graphics window
  219.      * @see #update
  220.      */
  221.     public void paint(Graphics g)
  222.     {
  223.         //Let the super class draw, then handle drawing the current image.
  224.         //Insert "ImageButton paint"
  225.         super.paint(g);
  226.  
  227.         if (image != null)
  228.             g.drawImage(image, 0, 0, this);
  229.     }
  230.  
  231.     //Inner class for handing mouse events.
  232.     //Insert "ImageButton Mouse Handling"
  233.     class Mouse extends MouseAdapter
  234.     {
  235.         public void mouseExited(MouseEvent event)
  236.         {
  237.             ImageButton_MouseExited(event);
  238.         }
  239.  
  240.         public void mouseEntered(MouseEvent event)
  241.         {
  242.             ImageButton_MouseEntered(event);
  243.         }
  244.  
  245.         public void mouseReleased(MouseEvent event)
  246.         {
  247.             ImageButton_MouseReleased(event);
  248.         }
  249.  
  250.         public void mousePressed(MouseEvent event)
  251.         {
  252.             ImageButton_MousePressed(event);
  253.         }
  254.     }
  255.  
  256.     protected void ImageButton_MousePressed(MouseEvent event)
  257.     {
  258.         isMouseDown = true;
  259.         handleMousePressed();
  260.     }
  261.  
  262.     protected void ImageButton_MouseReleased(MouseEvent event)
  263.     {
  264.         isMouseDown = false;
  265.         handleMouseRelease(isMouseInside);
  266.     }
  267.  
  268.     protected void ImageButton_MouseEntered(MouseEvent event)
  269.     {
  270.         isMouseInside = true;
  271.         handleRollover(isMouseInside, isMouseDown);
  272.     }
  273.  
  274.     protected void ImageButton_MouseExited(MouseEvent event)
  275.     {
  276.         isMouseInside = false;
  277.         handleRollover(isMouseInside, isMouseDown);
  278.     }
  279. }
  280.